package com.lin.java.tool.file.service.impl;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.java.tool.common.ex.BizException;
import com.lin.java.tool.common.model.DataPage;
import com.lin.java.tool.common.model.QueryPage;
import com.lin.java.tool.file.dao.mapper.SysFileConfigMapper;
import com.lin.java.tool.file.dao.po.SysFileConfigEntity;
import com.lin.java.tool.file.service.FileConfigService;
import com.lin.java.tool.file.service.vo.FileConfigVO;
import com.lin.java.tool.mybatis.utils.QueryPageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @作者: 林江
 * @创建时间: 2024/3/22
 * @功能:
 */
@Service
@Transactional
public class FileConfigServiceImpl extends ServiceImpl<SysFileConfigMapper, SysFileConfigEntity> implements FileConfigService {

    /**
     * 初始化对象存储参数
     */
    Map<String, SysFileConfigEntity> fileConfigs = new ConcurrentHashMap<>();

    @Override
    public DataPage<SysFileConfigEntity> listPage(QueryPage page) {
        return QueryPageUtil.lambdaPageQuery(this, page, queryWarpper -> {
            queryWarpper.eq(StringUtils.isNoneBlank(page.getKeyword()), SysFileConfigEntity::getPlatform, page.getKeyword());
        }, entity -> entity);
    }

    @Override
    public Boolean add(FileConfigVO body) {
        // 校验配置
        check(body.getId(), body.getPlatform(), body.getBucket());

        SysFileConfigEntity config = new SysFileConfigEntity();
        BeanUtils.copyProperties(body, config);
        config.setAdditionalParams(JSON.toJSONString(body.getAdditionalParams()));
        boolean save = save(config);

        return save;
    }

    @Override
    public Boolean edit(FileConfigVO body) {
        // 校验配置
        check(body.getId(), body.getPlatform(), body.getBucket());

        SysFileConfigEntity config = new SysFileConfigEntity();
        BeanUtils.copyProperties(body, config);
        config.setAdditionalParams(JSON.toJSONString(body.getAdditionalParams()));
        boolean update = updateById(config);

        return update;
    }

    @Override
    public Boolean deleteById(Long id) {
        boolean remove = removeById(id);

        return remove;
    }

    @Override
    public SysFileConfigEntity getByFileConfigKey(String fileConfigKey) {
        if (fileConfigs.containsKey(fileConfigKey)) {
            return fileConfigs.get(fileConfigKey);
        }
        return null;
    }

    @Override
    public void removeByFileConfigKey(String fileConfigKey) {
        if (fileConfigs.containsKey(fileConfigKey)) {
            fileConfigs.remove(fileConfigKey);
        }
    }

    @Override
    public List<SysFileConfigEntity> listByEnable() {
        List<SysFileConfigEntity> list = new ArrayList<>();
        for (String key : fileConfigs.keySet()) {
            if (key.endsWith(":1")) {
                list.add(fileConfigs.get(key));
            }
        }
        return list;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        boolean updateStatus = lambdaUpdate().set(SysFileConfigEntity::getStatus, status)
                .eq(SysFileConfigEntity::getId, id)
                .update();
    }

    /**
     * 数据校验
     *
     * @param id
     * @param platform
     * @param bucket
     */
    private void check(Long id,String platform, String bucket) {
        Long count = lambdaQuery().eq(SysFileConfigEntity::getPlatform, platform)
                .eq(SysFileConfigEntity::getBucket, bucket)
                .ne(SysFileConfigEntity::getId, id).count();
        if (count > 0) {
            throw new BizException("该平台【" + platform + "】已存在桶【" + bucket + "】");
        }
    }

}
